package com.dm.library.utils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:
 * 修 改 人: zhoulai
 * 修改时间: 2017/7/15 17:48
 */


public class ViewUtils {

    /**
     * 遍历布局，并禁用所有子控件
     *
     * @param viewGroup 布局对象
     */
    public static void disableSubControls(ViewGroup viewGroup) {
        viewGroup.setEnabled(false);
        viewGroup.setClickable(false);
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View v = viewGroup.getChildAt(i);
            v.setEnabled(false);
            v.setClickable(false);
            if (v instanceof ViewGroup) {
                if (v instanceof Spinner) {
                    Spinner spinner = (Spinner) v;
                    spinner.setClickable(false);
                    spinner.setEnabled(false);
                } else if (v instanceof ListView) {
                    (v).setClickable(false);
                    (v).setEnabled(false);
                } else {
                    disableSubControls((ViewGroup) v);
                }
            } else if (v instanceof EditText) {
                (v).setEnabled(false);
                (v).setFocusable(false);
                (v).setClickable(false);
            } else if (v instanceof Button) {
                (v).setEnabled(false);
            }
        }
    }

    public static String formatRealName(String realName) {
        if (null != realName && realName.length() > 1) {
            return "*" + realName.substring(1);
        } else {
            return realName;
        }
    }

    public static String formatIdCard(String idCard) {
        if (null != idCard && idCard.length() > 4) {
            return idCard.substring(0, 2) + " *** *** *** " + idCard.substring(idCard.length() - 2, idCard.length());
        } else {
            return idCard;
        }
    }


    public static String formatBankCard(String bankCard) {
        if (null != bankCard && bankCard.length() > 4) {
            return "**** **** **** " + bankCard.substring(bankCard.length() - 4, bankCard.length());
        } else {
            return bankCard;
        }
    }

    public static String formatPhone(String phone) {
        if (null != phone && phone.length() > 7 && !phone.contains("*")) {
            return phone.substring(0, 3) + "****" + phone.substring(phone.length() - 4, phone.length());
        } else {
            return phone;
        }
    }

    public static String formatDateToDay(String date) {
        if (null != date && date.length() > 10) {
            return date.substring(0, 10);
        } else {
            return date;
        }
    }

}
